/* ============================================================
   いろわけチューブ - Color Sort Puzzle  style.css
   ============================================================ */

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

:root {
  --bg1: #131028;
  --bg2: #2a1f4d;
  --panel: rgba(255, 255, 255, 0.06);
  --panel-border: rgba(255, 255, 255, 0.12);
  --text: #f0edff;
  --text-dim: #9d95c4;
  --accent: #7c6cff;
  --accent2: #ffd166;
  --radius: 14px;
  --per-row: 4;
  --tube-w: clamp(38px, min(15vw, calc((100vw - 32px - (var(--per-row) - 1) * 3vw) / var(--per-row))), 62px);
}

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(90% 60% at 15% 0%, rgba(124, 108, 255, 0.25) 0%, transparent 60%),
    radial-gradient(80% 55% at 90% 10%, rgba(242, 93, 170, 0.18) 0%, transparent 55%),
    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;
  overflow-x: hidden; /* 注ぎアニメで傾いたチューブがはみ出ても横スクロールさせない */
}

#app {
  min-height: calc(100dvh - var(--sgh-top, 0px) - var(--ad-space, 0px));
  display: flex;
  flex-direction: column;
  align-items: center;
  /* 上下のセーフエリアは足さない（上は共通ヘッダーの --sgh-top、下は #footer / --ad-space が含む） */
  padding: 0 env(safe-area-inset-right) 0 env(safe-area-inset-left);
}

/* ---------- ヘッダー ---------- */
#topbar {
  width: 100%;
  max-width: 900px;
  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(90deg, #ff8f8f, #ffd166, #7ee787, #6cc7ff, #c99cff);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  white-space: nowrap;
}

#diff-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;
}
#diff-badge:active { transform: scale(0.95); }

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

.icon-btn {
  width: 38px;
  height: 38px;
  font-size: 18px;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 10px;
  color: var(--text);
  cursor: pointer;
  display: grid;
  place-items: center;
}
.icon-btn:active { transform: scale(0.92); }

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

.status-box {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
  padding: 8px 10px;
  font-variant-numeric: tabular-nums;
}
.status-label { font-size: 12px; color: var(--text-dim); font-weight: 700; }
.status-value { font-size: 20px; font-weight: 800; letter-spacing: 0.04em; }

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

#board {
  display: flex;
  flex-direction: column;
  gap: clamp(22px, 5vh, 42px);
}

.board-row {
  display: flex;
  justify-content: center;
  gap: clamp(10px, 3vw, 26px);
}

.tube {
  position: relative;
  width: var(--tube-w);
  aspect-ratio: 10 / 31;
  background: transparent;
  border: none;
  padding: 0;
  cursor: pointer;
  transition: transform 0.16s ease;
  -webkit-tap-highlight-color: transparent;
}
.tube:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 4px;
  border-radius: 10px;
}
.tube.sel { transform: translateY(-12px); }
.tube.pouring { z-index: 6; pointer-events: none; }

/* 注いでいる液体のストリーム（液柱） */
.stream {
  position: fixed;
  z-index: 5;
  pointer-events: none;
  transform-origin: top center;
  animation: streamIn 0.14s ease-out;
}
@keyframes streamIn {
  from { transform: scaleY(0); }
}
.stream svg {
  display: block;
  width: 100%;
  height: 100%;
  transform-origin: top center;
  animation: streamWobble 0.36s ease-in-out infinite alternate;
}
@keyframes streamWobble {
  from { transform: scaleX(0.92); }
  to   { transform: scaleX(1.08); }
}
/* 注ぎ終わり: 液柱の尻尾が口から離れて落ちきる */
.stream.ending {
  transform-origin: bottom center;
  animation: streamOut 0.18s ease-in forwards;
}
@keyframes streamOut {
  to { transform: scaleY(0.05); opacity: 0.4; }
}

/* 着水点のしぶき */
.stream .splash {
  position: absolute;
  bottom: 0;
  left: 50%;
  width: 0;
  height: 0;
}
/* 波紋 */
.stream .splash::before {
  content: '';
  position: absolute;
  left: -11px;
  bottom: -3px;
  width: 22px;
  height: 7px;
  border-radius: 50%;
  background: var(--c);
  opacity: 0.5;
  animation: splashPulse 0.28s ease-in-out infinite alternate;
}
@keyframes splashPulse {
  from { transform: scale(0.8); opacity: 0.35; }
  to   { transform: scale(1.2); opacity: 0.6; }
}
/* 飛沫の粒 */
.stream .splash i {
  position: absolute;
  left: -2px;
  bottom: 0;
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: var(--c);
  opacity: 0;
  animation: splashDrop 0.5s ease-out infinite;
}
.stream .splash i:nth-child(1) { --dx: -11px; --dy: -14px; animation-duration: 0.42s; }
.stream .splash i:nth-child(2) { --dx: 10px;  --dy: -18px; animation-delay: 0.12s; }
.stream .splash i:nth-child(3) { --dx: -7px;  --dy: -22px; animation-delay: 0.25s; animation-duration: 0.55s; }
.stream .splash i:nth-child(4) { --dx: 13px;  --dy: -10px; animation-delay: 0.34s; animation-duration: 0.4s; }
@keyframes splashDrop {
  0%   { opacity: 0.9; transform: translate(0, 0) scale(1); }
  100% { opacity: 0; transform: translate(var(--dx, 8px), var(--dy, -14px)) scale(0.4); }
}
.tube.hint::before {
  content: '';
  position: absolute;
  inset: -6px;
  border-radius: 14px;
  border: 2px solid var(--accent2);
  animation: hintPulse 0.6s ease-in-out infinite alternate;
  pointer-events: none;
}
@keyframes hintPulse {
  from { opacity: 0.35; transform: scale(0.98); }
  to   { opacity: 1;    transform: scale(1.03); }
}
.tube.pop .glass { animation: pop 0.24s ease; }
@keyframes pop {
  40% { transform: scaleY(1.02) scaleX(1.06); }
}

/* ガラス部分 */
.glass, .segs {
  position: absolute;
  left: 0;
  right: 0;
  top: 6px;
  bottom: 0;
  border-radius: 6px 6px 999px 999px;
}
.glass {
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.10), rgba(255, 255, 255, 0.04) 30%, rgba(255, 255, 255, 0.08));
  border: 2px solid rgba(255, 255, 255, 0.28);
  border-top: none;
  box-shadow: inset 0 -6px 14px rgba(255, 255, 255, 0.08), 0 8px 18px rgba(0, 0, 0, 0.35);
  pointer-events: none;
  z-index: 2;
}
/* 口の縁 */
.glass::before {
  content: '';
  position: absolute;
  left: -5px;
  right: -5px;
  top: -6px;
  height: 8px;
  border-radius: 999px;
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.45), rgba(255, 255, 255, 0.12));
}
/* ハイライト */
.glass::after {
  content: '';
  position: absolute;
  top: 10%;
  left: 14%;
  width: 12%;
  height: 74%;
  border-radius: 999px;
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.35), rgba(255, 255, 255, 0.02));
}

.segs {
  overflow: hidden;
  display: flex;
  flex-direction: column-reverse;
  padding: 2px;
  z-index: 1;
}

.seg {
  flex: 0 0 auto;
  height: 25%;
  min-height: 0;
  transition: height 0.2s ease;
  display: grid;
  place-items: center;
  font-size: calc(var(--tube-w) * 0.34);
  color: rgba(255, 255, 255, 0.85);
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.45);
  overflow: hidden;
}
.seg:first-child { border-radius: 0 0 999px 999px; }
.seg.h0 { height: 0%; }

.tube.done .glass {
  border-color: rgba(126, 231, 135, 0.65);
  box-shadow: inset 0 -6px 14px rgba(255, 255, 255, 0.08), 0 0 16px rgba(126, 231, 135, 0.35);
}

/* ---------- ツールバー ---------- */
#toolbar {
  width: 100%;
  max-width: 520px;
  display: flex;
  justify-content: center;
  gap: 10px;
  padding: 6px 16px 10px;
}

.tool-btn {
  flex: 1;
  max-width: 150px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  font-family: inherit;
  font-size: 14px;
  font-weight: 700;
  color: var(--text);
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
  padding: 12px 8px;
  cursor: pointer;
}
.tool-btn:active { transform: scale(0.95); background: rgba(255, 255, 255, 0.12); }
.tool-icon { font-size: 16px; }

/* ---------- 結果オーバーレイ ---------- */
#result-overlay {
  position: fixed;
  inset: 0;
  background: rgba(10, 8, 24, 0.72);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  display: grid;
  place-items: center;
  z-index: 40;
  padding: 20px;
}

#result-box {
  width: min(92vw, 360px);
  background: linear-gradient(180deg, #2c2454, #1c1738);
  border: 1px solid var(--panel-border);
  border-radius: 20px;
  padding: 26px 22px;
  text-align: center;
  animation: rise 0.3s ease;
}
@keyframes rise {
  from { transform: translateY(24px); opacity: 0; }
  to   { transform: none; opacity: 1; }
}

#result-title {
  font-size: 30px;
  font-weight: 800;
  background: linear-gradient(90deg, #ffd166, #ff8f8f, #c99cff);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
}
#result-sub { margin-top: 4px; color: var(--text-dim); font-size: 14px; font-weight: 700; }

#result-stats {
  display: flex;
  justify-content: center;
  gap: 18px;
  margin: 18px 0 20px;
}
.rstat { display: flex; flex-direction: column; gap: 2px; }
.rstat-label { font-size: 11px; color: var(--text-dim); font-weight: 700; }
.rstat-value { font-size: 22px; font-weight: 800; font-variant-numeric: tabular-nums; }

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

.btn {
  font-family: inherit;
  font-size: 16px;
  font-weight: 800;
  border: none;
  border-radius: 999px;
  padding: 13px 18px;
  cursor: pointer;
}
.btn:active { transform: scale(0.96); }
.btn-primary {
  color: #1a1433;
  background: linear-gradient(90deg, #ffd166, #ffb35c);
  box-shadow: 0 6px 18px rgba(255, 209, 102, 0.3);
}
.btn-sub {
  color: var(--text);
  background: var(--panel);
  border: 1px solid var(--panel-border);
}

/* ---------- モーダル ---------- */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(10, 8, 24, 0.72);
  display: grid;
  place-items: center;
  z-index: 50;
  padding: 20px;
}

.modal-box {
  width: min(92vw, 400px);
  max-height: 84dvh;
  overflow-y: auto;
  background: linear-gradient(180deg, #2c2454, #1c1738);
  border: 1px solid var(--panel-border);
  border-radius: 20px;
  padding: 22px 20px;
}

.modal-title { font-size: 20px; font-weight: 800; text-align: center; margin-bottom: 14px; }

.menu-section { margin-bottom: 18px; }
.menu-caption {
  font-size: 12px;
  font-weight: 700;
  color: var(--text-dim);
  letter-spacing: 0.08em;
  margin-bottom: 8px;
}

.diff-buttons { display: flex; gap: 8px; }
.diff-btn {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  font-family: inherit;
  font-size: 14px;
  font-weight: 800;
  color: var(--text);
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 12px;
  padding: 10px 4px;
  cursor: pointer;
}
.diff-btn small { font-size: 10px; font-weight: 700; color: var(--text-dim); }
.diff-btn.active {
  border-color: var(--accent2);
  background: rgba(255, 209, 102, 0.12);
}
.diff-btn.active small, .diff-btn.active { color: var(--accent2); }

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

.toggle {
  position: relative;
  width: 46px;
  height: 26px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.14);
  border: 1px solid var(--panel-border);
  cursor: pointer;
  transition: background 0.2s;
}
.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); }
.toggle.on .knob { transform: translateX(20px); }

.record-row {
  display: flex;
  justify-content: space-between;
  padding: 5px 2px;
  font-size: 14px;
  font-weight: 700;
}
.record-row span:last-child { color: var(--accent2); font-variant-numeric: tabular-nums; }

.howto {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 8px;
  font-size: 13px;
  line-height: 1.6;
  color: #cfc9ea;
}
.howto li { padding-left: 1.1em; position: relative; }
.howto li::before { content: '•'; position: absolute; left: 0; color: var(--accent2); }

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

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

/* ---------- 紙吹雪 ---------- */
#fx {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100dvh;
  pointer-events: none;
  z-index: 45;
}

/* ---------- フッター ---------- */
#footer {
  width: 100%;
  text-align: center;
  font-size: 11px;
  color: var(--text-dim);
  padding: 8px 12px calc(10px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 2px;
}
#footer a { color: var(--text-dim); }
/* 広告があるときは下のセーフエリアを広告枠（--ad-space）が受け持つ */
html.has-ad-banner #footer { padding-bottom: 10px; }

.hidden { display: none !important; }

/* ---------- 極小幅対応 ---------- */
@media (max-width: 360px) {
  .tool-btn { font-size: 12px; padding: 10px 4px; white-space: nowrap; }
  .tool-icon { font-size: 14px; }
  #logo { font-size: 16px; }
}

/* ---------- 狭い高さ（横持ちスマホ）対応 ---------- */
@media (max-height: 560px) {
  :root { --tube-w: clamp(30px, 7vh, 44px); }
  #board { gap: 14px; }
  #status-bar { padding-top: 2px; }
}

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